<?php
   // ************************************************************************
   // **                                                                    **
   // **          This original software was designed and coded by          **
   // **                                                                    **
   // **                         David A. Goldsmith                         **
   // **                                                                    **
   // **                       dave106@wonderdave.com                       **
   // **                                                                    **
   // **                          March/April 2005                          **
   // **            Revised for Galileo High School, August 2009            **
   // **                                             July   2010            **
   // **                       piperd@galileoweb.org                        **	
   // **                                                                    **
   // ************************************************************************
?>


<html>
<title>Computer Lab Signup System Administration</title>

<link rel="stylesheet" href="LAB-Admin.css" type="text/css">

<center>
   <font color="#770000" size=5><b><u>Computer Lab Signup System Administration</u></b></font><br><br>
   <font color="#800080" size=4><b>Add / Edit / Delete Calendar Weeks</b></font>
</center>
<br><br><br>


<script language="JavaScript" type="text/javascript">
   function ConfirmDelete(Room)
   {
      return confirm('Are you sure that you want to permanently\n' +
                     'REMOVE this week\'s lab signup schedule for\n' +
                     ' ' + Room + '?\n\n' +
                     '( OK = Yes    Cancel = No )');
   }
</script>


<?php
   include("Configure.php");


   // In case the "register_globals" PHP directive is off, make sure all of the passed form variables are accessible
   $Date       =  mysql_real_escape_string($_POST['Date']);
   $DeleteWeek = $_POST['DeleteWeek'];
   $Lab        =  mysql_real_escape_string($_POST['Lab']);
   $Room       =  mysql_real_escape_string($_POST['Room']);
   $Submit     = $_POST['Submit'];
   $Type0      =  mysql_real_escape_string($_POST['Type0']);
   $Type1      =  mysql_real_escape_string($_POST['Type1']);
   $Type2      =  mysql_real_escape_string($_POST['Type2']);
   $Type3      =  mysql_real_escape_string($_POST['Type3']);
   $Type4      =  mysql_real_escape_string($_POST['Type4']);


   function ModifyCalendar($Date, $Type)
   {
      global $DB, $Room, $DeleteWeek;
      $Suffix = str_replace(" ", "_", $Room);
 
      // Determine if data are to be INSERTED or UPDATED in the "LabSignups$Room" table
      $Query = mysql_query("SELECT Date FROM LabSignups$Suffix WHERE Date='$Date'", $DB);
      $NumRecords = mysql_num_rows($Query);

      if ($DeleteWeek)
         $Query = mysql_query("DELETE FROM LabSignups$Suffix WHERE Date='$Date' LIMIT 1", $DB);
      else if ($NumRecords == 0)
         $Query = mysql_query("INSERT INTO LabSignups$Suffix (Date, Schedule) VALUES ('$Date', '$Type')", $DB);
      else
         $Query = mysql_query("UPDATE LabSignups$Suffix SET Schedule='$Type' WHERE Date='$Date'", $DB);
   }
	

   // See if a submission has been made of a calendar week to be edited
   if (($Submit) and ($Room) and ($Date))
   {
      for ($Counter=0; $Counter<=4; $Counter++)
      {
         // Advance to the next day of the week
         $Day = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime($Date)),
                                              date("d", strtotime($Date)) + $Counter,
                                              date("Y", strtotime($Date))));

         // Insert/Update/Delete a single day in the "LabSignups$Room" table
         if ($Counter == 0) { ModifyCalendar($Day, $Type0); }
         if ($Counter == 1) { ModifyCalendar($Day, $Type1); }
         if ($Counter == 2) { ModifyCalendar($Day, $Type2); }
         if ($Counter == 3) { ModifyCalendar($Day, $Type3); }
         if ($Counter == 4) { ModifyCalendar($Day, $Type4); }
      }
   }


   // Determine which computer lab weekly schedule should be displayed
   if ($Lab)
   	$Room = $Lab;
   $Lab = "$Room";
	  

   // If no ROOM has been set, find the first lab in the "Labs" table
   if (! $Room)
   {
      $Query = mysql_query("SELECT Room FROM Labs ORDER BY Room", $DB);
      $Data = mysql_fetch_array($Query);
      $Room = $Data["Room"];
   }

   $Suffix = str_replace(" ", "_", $Room); 

   // If no DATE has been set, determine the date of the Monday for this current week (or for the upcoming week,
   //  if it is now a weekend)
   if (! $Date)
   {
      $ThisFriday = date("Y-m-d 00:00:00", strtotime("this Friday"));
      $Date = date("Y-m-d", strtotime("last Monday", strtotime($ThisFriday)));
   }


   // Retrieve all of the computer labs from the "Labs" table
   $Query = mysql_query("SELECT Room FROM Labs ORDER BY Room", $DB);

   // Display a pulldown menu of all of the computer labs in the "Labs" table
   echo "<center><form name='LabForm' method='post' action='EditWeek.php'>";
   echo "<input type='hidden' name='Room' value='$Room'>";
   echo "<input type='hidden' name='Date' value='$Date'>";
   echo "<td><font face='Arial' size=5 color='#000000'><b>Schedule For:</b></font> &nbsp; &nbsp;";

   echo "<select name='Lab' onChange='submit()'>";
   while ($Data = mysql_fetch_array($Query))
   {
      $TempRoom = $Data["Room"];
      if ($Lab == "$TempRoom")
         echo "<option selected>$TempRoom";
      else
         echo "<option>$TempRoom";
   }
   echo "</select></td></form></center><br>";


   // Determine the the date of the Monday of LAST week
   $LastWeek = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime($Date)),
                                             date("d", strtotime($Date)) - 7,
                                             date("Y", strtotime($Date))));

   // Determine the the date of the Monday of NEXT week
   $NextWeek = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime($Date)),
                                             date("d", strtotime($Date)) + 7,
                                             date("Y", strtotime($Date))));


   // Display the "Last Week" and "Next Week" buttons
   echo "<table align=center border=0 cellpadding=0 cellspacing=0><tr>";

   echo "<form method='post' action='EditWeek.php'><td>";
   echo "<input type='hidden' name='Room' value='$Room'>";
   echo "<input type='hidden' name='Date' value='$LastWeek'>";
   echo "<input type='submit' value='<--- &nbsp; &nbsp; Last Week' ";
   echo "style='width:132px;color:#000000;font-weight:normal'></td>";
   echo "</td></form>";

   echo "<td width=25></td>";

   echo "<form method='post' action='EditWeek.php'><td>";
   echo "<input type='hidden' name='Room' value='$Room'>";
   echo "<input type='hidden' name='Date' value='$NextWeek'>";
   echo "<input type='submit' value='Next Week &nbsp; &nbsp; --->' ";
   echo "style='width:132px;color:#000000;font-weight:normal'></td>";
   echo "</td></form>";

   echo "</tr></table><br>";


   echo "<table align=center border=3 cellpadding=3 cellspacing=0 bordercolor='#800080'>";
   echo "<tr valign=top bgcolor='#000000'>";
   echo "<th width=200><font color='#ffffff'>Day / Date</font></th>";
   echo "<th width=200><font color='#ffffff'>Current Schedule</font></th>";
   echo "<th width=200><font color='#ffffff'>New Schedule</font></th>";
   echo "</tr>";


   // Determine if the week to be processed already exists in the "LabSignups$Room" table
   $Query = mysql_query("SELECT Date FROM LabSignups$Suffix WHERE Date='$Date'", $DB);
   $CannotDelete = 1 - mysql_num_rows($Query);


   for ($Counter=0; $Counter<=4; $Counter++)
   {
      // Advance to the next day of the week
      $Day = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime($Date)),
                                           date("d", strtotime($Date)) + $Counter,
                                           date("Y", strtotime($Date))));


      // Determine if any teachers have already signed up for one or more periods on the day being processed
      $Query = mysql_query("SELECT Date FROM LabSignups$Suffix
                            WHERE (Date='$Day') AND
                                  ((Signup1!='') OR (Signup2!='') OR (Signup3!='') OR (Signup4!='') OR
                                   (Signup5!='') OR (Signup6!='') OR (Signup7!='') OR (Signup8!=''))", $DB);
      $CannotDelete += mysql_num_rows($Query);


      // Retrieve the lab signup information from the "LabSignups$Room" table for the day being processed
      $Query1 = mysql_query("SELECT Schedule FROM LabSignups$Suffix WHERE Date='$Day'", $DB);
      $Data1 = mysql_fetch_array($Query1);

      $Schedule = $Data1["Schedule"];
      $ShowDay = date("F j, Y", strtotime($Day));
      $Weekday = date("l", strtotime($Day));


      if (! $Schedule)
         $Schedule = " &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -----&nbsp; Not Set &nbsp;-----";

      echo "<tr><td>$Weekday, $ShowDay</td>";
      echo "<td>$Schedule</td>";


      // Retrieve the daily schedules from the "LabDayTypes" table
      $Query3 = mysql_query("SELECT Schedule FROM LabDayTypes", $DB);

      // Display a pulldown menu of all of the daily schedules in the "LabDayTypes" table
      echo "<form name='ScheduleForm' method='post' action='EditWeek.php'>";
      echo "<input type='hidden' name='Room' value='$Room'>";
      echo "<input type='hidden' name='Date' value='$Date'>";

      echo "<td align=center><select name='Type$Counter'>";
      while ($Data3 = mysql_fetch_array($Query3))
      {
         $TempSchedule = $Data3["Schedule"];
         if ($Schedule == "$TempSchedule")
            echo "<option selected>$TempSchedule";
         else
            echo "<option>$TempSchedule";
      }
      echo "</select></td></tr>";
   }

   echo "</table><br>";


   echo "<table align=center border=0 cellpadding=0 cellspacing=0><tr><td valign=top height=50>";

   // Determine whether or not the entire current week can be removed; the week can only be deleted if NO teachers
   //  have signed up for any lab periods at any time during the week
   if ($CannotDelete == 0)
   {
      echo "<input type='checkbox' name='DeleteWeek' ";
      echo "onClick='if (checked) { return ConfirmDelete($Room) }'>";
      echo "<b>Delete Entire Week</b>";
   }
   else
      echo "<br><br><br>";

   echo "</td></tr></table>";


   // Display a button that saves the lab schedule for this week only
   echo "<center><input type='submit' name='Submit' value='Save Information' ";
   echo "style='width:130px;font-weight:bold;color:#006000'> &nbsp; &nbsp; &nbsp; &nbsp; ";

   // Display a button that cancels any unsaved changes for this week only and restarts the edit week process
   echo "<input type='button' value='Cancel' ";
   echo "style='width:80px;font-weight:bold;color:#c00000' ";
   echo "onClick=\"location.replace('EditWeek.php')\"> &nbsp; &nbsp; &nbsp; &nbsp; ";

   // Display a button that returns to the main menu (without saving any changes)
   echo "<input type='button' value='Return to Main Menu' ";
   echo "style='width:154px;font-weight:bold;color:#000090' ";
   echo "onClick=\"location.replace('AdminMenu.php')\">";
   echo "</center></form><br>";
?>


</body>

</html>
